package cn.lixin.pet.controller;

import cn.lixin.basic.exception.BusinessException;
import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.LoginContext;
import cn.lixin.basic.utils.PageList;
import cn.lixin.pet.domain.Pet;
import cn.lixin.pet.domain.SearchMasterMsg;
import cn.lixin.pet.mapper.SearchMasterMsgMapper;
import cn.lixin.pet.query.SearchMasterMsgQuery;
import cn.lixin.pet.service.ISearchMasterMsgService;
import cn.lixin.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.loadAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request); // 获取当前登录人信息
            searchMasterMsgService.publish(searchMasterMsg,logininfo.getId()); // 将信息和登录人信息传到service
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！！");
        }
    }

    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request); // 获取当前登录人信息
            return searchMasterMsgService.pending(searchMasterMsgQuery,logininfo.getId()); // 将信息和登录人信息传到service
        } catch (BusinessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request); // 获取当前登录人信息
            return searchMasterMsgService.processed(searchMasterMsgQuery,logininfo.getId()); // 将信息和登录人信息传到service
        } catch (BusinessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/queryUserMsg")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request); // 获取当前登录人信息
            return searchMasterMsgService.queryUserMsg(searchMasterMsgQuery,logininfo.getId()); // 将信息和登录人信息传到service
        } catch (BusinessException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 处理 未处理的信息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLoginInfo(request); // 获取当前登录人信息
            searchMasterMsgService.handle(pet,logininfo.getId()); // 将信息和登录人信息传到service
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }
}
